package uit.nfcmenu.db.adapter;

import java.util.ArrayList;

import uit.nfcmenu.db.model.ItemModel;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.util.Log;


public class ItemAdapter extends DatabaseAdapter {
	String tag = ItemAdapter.class.getSimpleName();

	public ItemAdapter(Context context) {
		super(context);
	}
	
	public ItemModel createItemModel(Cursor cursor) {
		ItemModel model = new ItemModel();
		model.setId(cursor.getInt(cursor.getColumnIndex(DbDefines.TableItem.itemId))); 
		model.setName(cursor.getString(cursor.getColumnIndex(DbDefines.TableItem.itemName)));
		model.setMenuId(cursor.getInt(cursor.getColumnIndex(DbDefines.TableItem.menuId)));
		model.setImageLink(cursor.getString(cursor.getColumnIndex(DbDefines.TableItem.imageLink)));
		model.setDescription(cursor.getString(cursor.getColumnIndex(DbDefines.TableItem.description)));
		model.setPrice(Double.parseDouble(cursor.getString(cursor.getColumnIndex(DbDefines.TableItem.price)))); 
		model.setMoneyType(cursor.getString(cursor.getColumnIndex(DbDefines.TableItem.moneyType)));
		model.setDiscount(Double.parseDouble(cursor.getString(cursor.getColumnIndex(DbDefines.TableItem.discount)))); 
		//Bitmap bitmap = ConvertImg.getBitmap(cursor.getBlob(cursor.getColumnIndex(DbDefines.TableItem.bitmap)));
		//model.setBitmap(bitmap);
		String bitmapString = cursor.getString(cursor.getColumnIndex(DbDefines.TableItem.bitmap));
		model.setBitmap(bitmapString);
		
		Log.w(tag, "load model="+model.toString());
		return model;
	}

	private ContentValues createContentByModel(ItemModel model) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(DbDefines.TableItem.itemId, model.getId());
		contentValues.put(DbDefines.TableItem.itemName, model.getName());
		contentValues.put(DbDefines.TableItem.menuId, model.getMenuId());
		contentValues.put(DbDefines.TableItem.imageLink, model.getImageLink());
		contentValues.put(DbDefines.TableItem.price, model.getPrice());
		contentValues.put(DbDefines.TableItem.moneyType, model.getMoneyType());
		contentValues.put(DbDefines.TableItem.description, model.getDescription());
		contentValues.put(DbDefines.TableItem.discount, model.getDiscount());
//		byte[] img = ConvertImg.getBytes(model.getBitmap());
//		contentValues.put(DbDefines.TableItem.bitmap, img);
		contentValues.put(DbDefines.TableItem.bitmap, model.getBitmapString());
//		Log.i(tag, "save bitmapString="+model.getBitmapString().substring(0, 50));
		
		return contentValues;
	}

	
	public int insertItemModel(ItemModel model) {
		int value = -1;
		if (model == null) {
			return value;
		}

		this.open();

		try {
			// Assign values to add...
			ContentValues contentValues = createContentByModel(model);

			// Add to database
			value = (int) this.mDb.insert(DbDefines.ItemModel, "",
					contentValues);
		} finally {
			this.close();
		}

		return value;
	}

	public int insertItemModels(ArrayList<ItemModel> models) {
		if (models == null) {
			return 0;
		}

		int value = 0;
		int result = -1;
		this.open();

		try {
			// Assign values to add...
			ContentValues contentValues = null;
			for (ItemModel model : models) {
				contentValues = createContentByModel(model);

				// Add to database
				result = (int) this.mDb.insert(DbDefines.ItemModel, "", contentValues);
				Log.i(tag, "insertItemModels() result="+result+", model: "+model.toString());
				if (result != -1) {
					value++;
				}
			}
		} finally {
			this.close();
		}

		return value;
	}

	public int deleteItemModel(int itemID) {
		int result = 0;
		this.open();
		try {
			result = this.mDb.delete(DbDefines.ItemModel,
					DbDefines.TableItem.itemId + " = " + itemID, null);
		} finally {
			this.close();
		}
		return result;
	}

	public synchronized int updateItemModel(ItemModel model) {
		int value = -1;
		this.open();
		try {
			// Assign values to add...
			ContentValues contentValues = createContentByModel(model);
			// Add to database
			//Log.i(tag, "update model item "+model.toString());
			value = (int) this.mDb.update(DbDefines.ItemModel, contentValues,
					DbDefines.TableItem.itemId + " = " + model.getId(),
					null);
			if (value == 0) {
				this.close();
			}
		} finally {
			this.close();
		}
		return value;
	}
	
	public ItemModel loadItemModel(int itemID) {
		this.open();
		// getting data from database
		Cursor cursor = null;
		ItemModel model = null;
		try {
			cursor = this.mDb.query(DbDefines.ItemModel, new String[] {
					DbDefines.TableItem.itemId, DbDefines.TableItem.itemName,
					DbDefines.TableItem.menuId, DbDefines.TableItem.imageLink,
					DbDefines.TableItem.price, DbDefines.TableItem.description,
					DbDefines.TableItem.discount, DbDefines.TableItem.bitmap, 
					DbDefines.TableItem.moneyType }, 
					DbDefines.TableItem.itemId + " = " + itemID , 
					null, "", "", "", "1");

			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				model = createItemModel(cursor);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return model;
	}

	public ArrayList<ItemModel> selectAll() {
		this.open();

		// getting data from database
		Cursor cursor = null;
		ItemModel model = null;
		ArrayList<ItemModel> models = new ArrayList<ItemModel>();
		try {
			cursor = this.mDb.query(DbDefines.ItemModel, new String[] {
					DbDefines.TableItem.itemId, DbDefines.TableItem.itemName,
					DbDefines.TableItem.menuId, DbDefines.TableItem.imageLink,
					DbDefines.TableItem.price, DbDefines.TableItem.description,
					DbDefines.TableItem.discount, DbDefines.TableItem.bitmap, 
					DbDefines.TableItem.moneyType }, 
					"", null, "", "", "", "");

			while (cursor.moveToNext()) {
				model = createItemModel(cursor);
				models.add(model);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return models;
	}

	public String loadStringBitmapById(int itemId) {
		this.open();

		// getting data from database
		Cursor cursor = null;
		String model = null;
		try {
			cursor = this.mDb.query(DbDefines.ItemModel, new String[] {
					DbDefines.TableItem.bitmap}, "", null, "", "", "", "");

			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				model = cursor.getString(cursor.getColumnIndex(DbDefines.TableItem.bitmap));
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		Log.i(tag, "loadStringBitmapById("+itemId+")"+(model==null?"null"
				:(model.subSequence(0, 10)+"..")));
		return model;
	}

	// =================================
	// MORE
	// =================================

	public ArrayList<ItemModel> getItemsByMenu(int menuId) {
		this.open();
		
		// getting data from database
		Cursor cursor = null;
		ItemModel model = null;
		ArrayList<ItemModel> models = new ArrayList<ItemModel>();
		try {
			cursor = this.mDb.query(DbDefines.ItemModel, new String[] {
					DbDefines.TableItem.itemId, DbDefines.TableItem.itemName,
					DbDefines.TableItem.menuId, DbDefines.TableItem.imageLink,
					DbDefines.TableItem.price, DbDefines.TableItem.description,
					DbDefines.TableItem.discount, DbDefines.TableItem.bitmap, 
					DbDefines.TableItem.moneyType }, 
					DbDefines.TableItem.menuId + " = " + menuId, 
					null, "", "", "", "");

			while (cursor.moveToNext()) {
				model = createItemModel(cursor);
				models.add(model);
			}

		} catch (SQLiteException e) {
			e.printStackTrace();

		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}

		return models;
	}
	
	/**
	 * Delete database
	 */
	public void deleteDatabase() {
		this.open();
		mDb.execSQL("DELETE FROM "+DbDefines.ItemModel);
		this.close();
	}
}
